#include<bits/stdc++.h>
#define int long long 
using namespace std;
inline int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-'){
			f=-1;
		}
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	return x*f;
}
const int N=1e5+10;
int b[5];
struct fg{
	int x,y,sum;
}a[N];
struct fg1{
	int val,id;
}a1[100];
bool cmp(fg x,fg y){
	if(x.sum==y.sum){
		return x.y<y.y;
	}
	return x.sum<y.sum;
}
bool cmp1(fg1 x,fg1 y){
	if(x.val==y.val){
		return x.id<y.id;
	}
	return x.val<y.val;
}
int chack(int x,int y,int z){
	return a[x].sum+a[y].sum+a[z].sum-min(min(a[x].y,a[y].y),a[z].y);
}

signed main(){
	freopen("boast.in","r",stdin);
	freopen("boast.out","w",stdout);
	int n=read(),ans;
	for(int i=1;i<=n;i++){
		a[i].x=read(),a[i].y=read();
		a[i].sum=a[i].x+a[i].y;
	} 
	sort(a+1,a+n+1,cmp);
	ans=a[1].sum+a[2].sum+a[3].sum-min(min(a[1].y,a[2].y),a[3].y);
	if(n<=100){
		for(int i=1;i<=n;i++){
			for(int j=i+1;j<=n;j++){
				for(int k=j+1;k<=n;k++){
					ans=min(ans,a[i].sum+a[j].sum+a[k].sum-min(min(a[i].y,a[j].y),a[k].y));
				}
			}
		}
		printf("%lld",ans);
		return 0;
	}
	b[1]=1,b[2]=2,b[3]=3;
	for(int i=4;i<=n;i++){
		a1[1].val=chack(b[1],b[2],b[3]),a1[2].val=chack(i,b[2],b[3]),a1[3].val=chack(b[1],i,b[3]),a1[4].val=chack(b[1],b[2],i);
		for(int i=1;i<=4;i++)a1[i].id=i;
		sort(a1+1,a1+5,cmp1);
		b[a1[1].id-1]=i;
		ans=min(ans,a1[1].val);
	}
	printf("%lld",ans);
    return 0;
}

